package com.zoke.password.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sevenheaven.iosswitch.ShSwitchView;
import com.zoke.password.Conf;
import com.zoke.password.R;
import com.zoke.password.base.BaseActivity;
import com.zoke.password.model.User;
import com.zoke.password.tools.PersistTool;

import org.xutils.common.util.LogUtil;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import io.nebulas.Constants;
import io.nebulas.api.SmartContracts;
import io.nebulas.model.GoodsModel;
import io.nebulas.utils.Util;

@ContentView(R.layout.activity_settings)
public class SettingsActivity extends BaseActivity {
    @ViewInject(R.id.tv_title)
    TextView tv_title;
    @ViewInject(R.id.iv_back)
    ImageView iv_back;
    @ViewInject(R.id.sh_switch)
    ShSwitchView switchView;

    String password = "";
    private String serialNumber = "";
    @ViewInject(R.id.rl_edit)
    View rl_edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tv_title.setText(R.string.settings);
        iv_back.setVisibility(View.VISIBLE);
        if (User.getUser() != null) {
            password = User.getUser().password;
        }
        boolean isOpen = PersistTool.getBoolean(Conf.KEY_PASSWORD, false);
        if (TextUtils.isEmpty(password)) isOpen = false;
        switchView.setOn(isOpen, true);
        switchView.setOnSwitchStateChangeListener(new ShSwitchView.OnSwitchStateChangeListener() {
            @Override
            public void onSwitchStateChange(boolean isOn) {
                PersistTool.saveBoolean(Conf.KEY_PASSWORD, isOn);
                if (isOn && TextUtils.isEmpty(password)) {
                    PatternLockActivity.open(SettingsActivity.this, 1);
                }
            }
        });
        rl_edit.setVisibility(TextUtils.isEmpty(password) ? View.GONE : View.VISIBLE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case PatternLockActivity.LOCK:
                //设置完成 是否请求接口 去存储
                if (isEdit && data != null) {
                    isEdit = false;
                    PatternLockActivity.open(SettingsActivity.this, 1);
                } else {
                    LogUtil.e("data ==?null   " + data);
                }
                break;
        }
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        if (mImmersionBar != null)
            mImmersionBar.statusBarDarkFont(true)
                    .statusBarView(R.id.status)
                    .init();
    }


    private boolean isEdit;

    @Event({R.id.iv_back, R.id.rl_edit, R.id.rl_pay, R.id.rl_help})
    private void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.rl_edit:
                isEdit = true;
                PatternLockActivity.open(this, 0);
                break;
            case R.id.rl_pay:
                //捐赠nas
                serialNumber = Util.getRandomCode(Constants.RANDOM_LENGTH);
                String to = "n1GXAxQ3K2uUweXNCZHE1no8P7BeB98hPAm";//入账钱包地址，钱包地址，钱包地址 我的地址 我的地址
                GoodsModel goods = new GoodsModel();
                goods.name = "捐赠作者nas";
                goods.desc = "好心人真多";
                SmartContracts.pay(this, Constants.MAIN_NET, goods, to, "2000000000000000000", serialNumber);
                break;
            case R.id.rl_help:
                WebActivity.open(this, "新手指引教程", "file:///android_asset/新手指引教程.html");
                break;
        }
    }
}
